import { reactive, computed } from 'vue';
import { useStore } from 'vuex';
import { userData, getPersonalRepair, getEnterpriseMember, postLogout, getPersonalPerformance } from '@/api/server';
import { apiService } from '@/api/apiService';
import { MEMBER_WORK_STATUS } from '@/consts/status.consts';
import Cookies from 'js-cookie';
import { SET_IS_LOGIN, SET_USER_ROLES } from '@/store/mutation-types';
import { useRouter } from 'vue-router';
import { selfI18n } from '@/i18n';
export function usePersonalSetup () {
    const personal_state = reactive({
        userinfo: {},
        userstatus: '3',
        user_repair_data: {}
    });

    const getUserInfo = () => {
        apiService({
            api: userData,
            param: {},
            success: (data) => {
                personal_state.userinfo = data.value;
            }
        });
    };

    const getMemberStatus = () => {
        apiService({
            api: getEnterpriseMember,
            param: { account: Cookies.get('username') },
            success: (data) => {
                personal_state.userstatus = data.value.status;
            }
        });
    };
    const getMyRepairOrderInfo = () => {
        apiService({
            api: getPersonalRepair,
            param: { username: Cookies.get('username') },
            success: (data) => {
                Object.assign(personal_state.user_repair_data, data.value);
            }
        });
    };
    // 获取个人报修绩效
    const getPersonalRepairPerformance = () => {
        apiService({
            api: getPersonalPerformance,
            param: { username: Cookies.get('username') },
            success: (data) => {
                Object.assign(personal_state.user_repair_data, data.value);
            }
        });
    };
    const store = useStore();
    const router = useRouter();
    const logout = () => {
        apiService({
            api: postLogout,
            param: { username: Cookies.get('username') },
            success: () => {
                const userinfo_cookies = [
                    'phone',
                    // 'openid',
                    'vercode',
                    'locale',
                    'username',
                    'NCCLOUD_TOKEN',
                    'password',
                    'timezone',
                    'roles',
                    '_id'
                ];
                userinfo_cookies.forEach((child) => {
                    Cookies.remove(child);
                });
                store.commit(SET_USER_ROLES, []);
                store.commit(SET_IS_LOGIN, false);
                store.commit('SET_USER_INFO', {});

                router.push('/login');
            }
        });
    };
    const member_status_text = computed(() => {
        if (!personal_state.userstatus) {
            personal_state.userstatus = '0';
        }
        return selfI18n(MEMBER_WORK_STATUS[personal_state.userstatus]);
    });
    const message_number = computed(() => {
        return store.state.system_message_priview_number === 0 ? '' : store.state.system_message_priview_number;
    });
    return {
        logout,
        message_number,
        member_status_text,
        personal_state,
        getUserInfo,
        getMemberStatus,
        getPersonalRepairPerformance,
        getMyRepairOrderInfo
    };
}
